<template>
  <div class="course-detail" id="detail">
    <div class="course-detail-box">
       <div class="course-detial-main">
          <div class="course-detail-content w1200">
            <div class="header-wrap" v-if="isMenu">
              <div class="header w1200">
                <div class="header-l">
                  <span v-for="(item,i) in tabArr" :Key="i" :class="{on: i === curIndex}" @click="handleTab(i,0)">{{item}}</span>
                </div>
                <div class="header-r">
                  <span class="price">￥{{data.fee}}元</span>
                  <div class="btn">
                    <template v-if="data.can_pay === 0">
                          <span class="btn-item no-pay">立即报名</span>
                        </template>
                        <template v-if="data.can_pay === 1">
                          <template v-if="data.has_pay === 0">
                            <span class="btn-item" @click="handlePay">立即报名</span>
                          </template>
                          <template v-if="data.has_pay === 1">
                            <span class="btn-item has-pay">已购买</span>
                          </template>
                        </template>
                  </div>
                </div>
              </div>
            </div>
            <div class="bread w1200">
                <span><router-link to="/course-center">课程中心</router-link><a-icon type="right" /></span>
                <span @click="handleDetail">课程详情<a-icon type="right" /></span>
                <span>{{data.title}}</span>
            </div>
            <div class="course-info">
                <div class="course-info-l">
                <img :src="data.pic" />
                </div>
                <div class="course-info-r">
                <h3 class="title">{{data.title}}</h3>
                <p class="desc">{{data.desc}}</p>
                <fieldset>
                    <legend>课程信息</legend>
                    <p class="course-list">
                    <span>课时：{{data.course_time}}课时</span>
                    <span>有效期：{{data.add_time}}</span>
                    </p>
                </fieldset>
                <p class="tags">{{data.type === 0 ? '直播': '录播' }}</p>
                    <div class="price">
                    <span class="price-l">￥{{data.fee}}元</span>
                    <span class="price-r">
                        <img src="@/assets/home/course-detail-icon.png" />{{data.num}}人已购买
                    </span>
                    </div>
                    <div class="btn">
                      <template v-if="data.can_pay === 0">
                        <span class="btn-item no-pay">立即报名</span>
                        <span class="btn-tips">直播中，暂时无法购买。请等待直播完成</span>
                      </template>
                      <template v-if="data.can_pay === 1">
                        <template v-if="data.has_pay === 0">
                          <span class="btn-item" @click="handlePay">立即报名</span>
                        </template>
                        <template v-if="data.has_pay === 1">
                          <span class="btn-item has-pay">已购买</span>
                        </template>
                      </template>
                    </div>
                </div>
            </div>
          </div>
        </div>
        <!--conent-->
          <div class="course-detail-list w1200">
            <div class="course-detail-list-item">
                <div class="list-tab">
                <span v-for="(item,i) in tabArr" :Key="i" :class="{on: i === curIndex}" @click="handleTab(i,1)">{{item}}</span>
                </div>
                <div class="list-info" v-if="isShow">
                  <div v-html="data.content" v-show="curIndex === 0"></div>
                  <course-info v-show="curIndex === 1" :data="data.sub_course" :type="data.type" @enterRoom="enterRoom"/>
                  <course-comment v-if="curIndex === 2" :data="commentData"/>
                  <div class="main-page" v-if="commentData.length > 0 && totalPage > 1">
                    <template>
                      <a-pagination v-model="query.page" :total="total" show-less-items />
                    </template>
                  </div>
                </div>
            </div>
          </div>
       <div class="course-footer">
         <p class="menu">
            <a>关于我们</a>
            <a>联系我们</a>
            <a>帮助中心</a>
            <router-link to="/teacher-login">教师登录</router-link>
         </p>
         <p class="copy">
            <span>客服电话：400-188-8181</span>
            <span>Copyright © 2020 </span>
            <span>北京阳光易百教育科技有限公司</span>
            <span>版权所有</span>
            <span>京ICP备18003721号</span>
         </p>
       </div>
    </div>
    <div class="pay" v-if="isPay">
      <div class="pay-pop">
        <img src="@/assets/home/close-icon.png" class="close" @click="handleClose" />
        <p class="tips">易学派在线快捷支付</p>
        <p class="price">￥{{data.fee}}</p>
        <p class="code">
          <img :src="code" />
        </p>
        <div class="pay-money">
          <img src="@/assets/home/zfb-icon.png" />
          <img src="@/assets/home/wx-icon.png" />
          <p class="pic"><span>微信，支付宝</span><span>扫一扫去付款</span></p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  Pagination,
  Icon
} from "ant-design-vue"
import courseInfo from './course-info.vue'
import courseComment from './course-comment.vue'
import mTypes from "@/store/mutationsTypes"
import { mapState, mapMutations } from "vuex"
import { enterRoom } from "@/utils/tools"
import {
  courseDetail,
  coursePay,
  getappRaise
} from "@/services"
export default {
  components: {
    'course-info': courseInfo,
    'course-comment': courseComment,
    'a-pagination': Pagination
  },
  data () {
    return {
      tabArr: ['课程介绍', '课程大纲', '课程评价'],
      curIndex: 0,
      current: 1,
      total: 10,
      totalPage: 0,
      isMenu: false,
      data: {},
      commentData: [],
      isShow: false,
      isPay: false,
      code: '',
      scrollTop: 0,
      query: {
        page: 1,
        id: '',
        size: 10
      }
    }
  },
  computed: {
    ...mapState({
      userInfo: state => state.userInfo
    })
  },
  created () {
    if (this.$route.query.index) {
      this.curIndex = +this.$route.query.index
    }
    this.getData()
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll, true)
  },
  destroyed () {
    window.removeEventListener('scroll', this.handleScroll, true)
  },
  methods: {
    ...mapMutations([mTypes.loginModal]),
    handleTab (i, type) {
      this.curIndex = i
      if (type === 0) {
        document.getElementById('app').scrollTop = 0
      }
      if (i === 2) {
        this.geRaise()
      }
    },
    getData () {
      courseDetail({ id: +this.$route.query.id }).then((r) => {
        if (r.code == 1) {
          this.data = r.data
          this.isShow = true
          this.isPay = false
          console.log(r.data.appraise)
        }
      })
    },
    geRaise () {
      let params = {
        page: this.query.page,
        size: this.query.size,
        id: +this.$route.query.id
      }
      getappRaise(params).then(r => {
        if (r.code === 1) {
          this.commentData = r.data.list
          console.log(this.commentData)
          this.total = r.data.total
          this.totalPage = Math.ceil(this.total / this.query.size)
          console.log(this.totalPage + '_______________')
        }
      })
    },
    handlePay () {
      if (!this.$store.state.userInfo.username) {
        this[mTypes.loginModal](true)
        return
      }
      this.getPay()
    },
    getPay () {
      coursePay({ id: this.data.id }).then(r => {
        if (r.code === 1) {
          this.code = r.data.qrcodeUrl
          this.isPay = true
        }
      })
    },
    handleScroll () {
      if (document.getElementById('app').scrollTop > 640) {
        this.isMenu = true
      } else {
        this.isMenu = false
      }
    },
    handleClose () {
      this.isPay = false
      this.getData()
    },
    enterRoom (data) {
      enterRoom(data, this)
    },
    handleDetail () {
      console.log('不跳转重复路由')
      this.$router.push({
        path: '/courseDetail',
        query: {
          id: this.$route.query.id
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.course-detail {
  width: 100%;
  min-height: 100%;
  background: #F0F2F4;
}
.course-detial-main {
  margin: 0 auto;
  background: #fff;
  padding-bottom: 50px;
  .header-wrap {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    height: 70px;
    background: #fff;
    z-index: 100;
  }
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    &-l {
        display: flex;
        align-items: center;
        .on {
          position: relative;
          color: #07997F;
          &::after {
            display: block;
            content:'';
            position: absolute;
            left:50%;
            bottom: -10px;
            transform: translateX(-50%);
            width: 15px;
            height: 3px;
            background: #07997F;
            border-radius: 2px;
          }
        }
        span {
          font-size: 20px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          line-height: 26px;
          color: #000000;
          margin-right: 50px;
          cursor: pointer;
        }
      }
      &-r {
        display: flex;
        align-items: center;
        .price {
            font-size: 28px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            line-height: 37px;
            color: #FA700D;
        }
        .btn {
          width: 260px;
          height: 70px;
          margin-left: 40px;
          &-item {
            display: block;
            font-size: 24px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #FFFFFF;
            width: 260px;
            height: 70px;
            line-height: 70px;
            text-align: center;
            background: linear-gradient(90deg, #F7A015 0%, #FA670B 100%);
            cursor: pointer;
          }
          .no-pay {
            color: #ccc !important;
            background: #EBEBEB !important;
            box-shadow: 0 0 0 rgba(0,0,0,0) !important;
          }
          .has-pay {
            color: #07997F !important;
            background: #ECF7F5 !important;
            border: 1px solid #07997F !important;
            box-shadow: 0 0 0 rgba(0,0,0,0) !important;
          }
        }
      }
  }
  .bread {
    padding-top: 21px;
    padding-bottom: 22px;
    span {
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #333333;
      margin-right: 3px;
      cursor: pointer;
      a {
        color: #333;
      }
    }
  }
  .course-info {
    display: flex;
    &-l {
      width: 50%;
      height: 338px;
      margin-right: 40px;
      overflow: hidden;
      img {
        width: 100%;
        height: auto;
      }
    }
    &-r {
      flex: 1;
      .title {
        font-size: 24px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        line-height: 31px;
        color: #000000;
      }
      .desc {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        line-height: 20px;
        min-height: 40px;
        color: #B2B2B2;
        display: -webkit-box;
        overflow: hidden;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-top: 12px;
        word-break: break-all;
      }
      fieldset {
        margin-top: 10px;
        padding-left: 20px;
        padding-right: 0;
        border: 1px solid #E6E6E6;
        border-radius: 4px;
        legend {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          line-height: 19px;
          color: #07997F;
          width: 69px;
          padding-left: 5px;
          margin-bottom: 0;
        }
        .course-list {
          padding: 5px 0 13px;
          span {
            font-size: 14px;
            font-family: Microsoft YaHei;
            font-weight: 400;
            line-height: 19px;
            color: #333333;
            margin-right: 30px;
          }
        }
      }
      .tags {
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        line-height: 19px;
        color: #097662;
        background: #E9F4F2;
        border-radius: 6px;
        width: 66px;
        text-align: center;
        padding: 6px 0;
        margin-top: 12px;
      }
      .price {
        display: flex;
        align-items: center;
        margin-top: 18px;
        &-l {
          font-size: 28px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          line-height: 37px;
          color: #FA700D;
        }
        &-r {
          display: flex;
          align-items: center;
          margin-left: 30px;
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          line-height: 21px;
          color: #ADADAD;
          img {
            width: 14px;
            height: 15px;
            vertical-align: middle;
            margin-right: 3px;
          }
        }
      }
      .btn {
        display: flex;
        align-items: center;
        margin-top: 20px;
        .no-pay {
          color: #ccc !important;
          background: #EBEBEB !important;
          box-shadow: 0 0 0 rgba(0,0,0,0) !important;
        }
        .has-pay {
          color: #07997F !important;
          background: #ECF7F5 !important;
          border: 1px solid #07997F !important;
          box-shadow: 0 0 0 rgba(0,0,0,0) !important;
        }
        &-item {
          display: block;
          width: 300px;
          height: 70px;
          line-height: 70px;
          text-align: center;
          font-size: 24px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #FFFFFF;
          background: linear-gradient(90deg, #F7A015 0%, #FA670B 100%);
          box-shadow: 0px 3px 6px rgba(250, 110, 12, 0.3);
          opacity: 1;
          border-radius: 6px;
          cursor: pointer;
        }
        &-tips {
          display: block;
          font-size: 16px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          line-height: 21px;
          color: #FA700D;
          max-width: 190px;
          margin-left: 30px;
        }
      }
    }
  }
}
.course-detail-list {
    min-height: 500px;
    background: #fff;
    border-radius: 10px;
    margin-top: 22px;
    &-item {
      padding: 40px 50px 0;
      .list-tab {
        display: flex;
        align-items: center;
        .on {
          position: relative;
          color: #07997F;
          &::after {
            display: block;
            content:'';
            position: absolute;
            left:50%;
            bottom: -10px;
            transform: translateX(-50%);
            width: 15px;
            height: 3px;
            background: #07997F;
            border-radius: 2px;
          }
        }
        span {
          font-size: 20px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          line-height: 26px;
          color: #000000;
          margin-right: 50px;
          cursor: pointer;
        }
      }
      .list-info {
        margin-top: 49px;
        img {
          width: 100%;
          height: auto;
        }
        .list-info-page {
          text-align: center;
          margin-top: 40px;
        }
      }
    }
  }
.course-footer {
  width: 100%;
  background: #fff;
  text-align: center;
  margin-top: 30px;
  padding: 30px 0;
  .menu {
    a {
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 24px;
      color: #8B95A0;
      margin-right: 53px;
      cursor: pointer;
      &:last-child {
        margin-right: 0;
      }
    }
  }
  .copy {
    margin-top: 13px;
    span {
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #9EA6B0;
    }
  }
}
.main-page {
  text-align: center;
  margin-top: 40px;
  margin-bottom: 36px;
}
/deep/.ant-pagination-prev,/deep/.ant-pagination-next {
  width: 30px;
  height: 30px;
  border-color: #EFEFEF;
  // i {
  //   display: none;
  // }
}
/deep/.ant-pagination-item {
  width: 30px;
  height: 30px;
  border-color: #EFEFEF;
  border-radius: 6px;
}
/deep/.ant-pagination-item-active {
  background: #27BFA3;
  border: none;
  a {
    color: #fff;
  }
}
/deep/.ant-pagination-item-active {
  &:hover {
    a {
      color: #fff;
    }
  }
}
.pay {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.5);
  &-pop {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    width: 380px;
    height: 430px;
    background: #fff;
    border-radius: 10px;
    text-align: center;
    .close {
      position: absolute;
      right: 20px;
      top: 20px;
      width: 20px;
      height: 20px;
      cursor: pointer;
    }
    .tips {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 26px;
      color: #333333;
      padding-top: 20px;
    }
    .price {
      font-size: 40px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      line-height: 52px;
      color: #FA700D;
      margin-top: 27px;
    }
    .code {
      width: 180px;
      height: 180px;
      margin: 20px auto 30px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .pay-money {
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 40px;
        height: 40px;
        margin-right: 8px;
      }
      .pic {
        span {
          display: block;
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          line-height: 19px;
          color: #BABABA;
        }
      }
    }
    .pay-btn {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 70px;
      line-height: 70px;
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #FFFFFF;
      background: #27BFA3;
      border-radius: 0px 0px 10px 10px;
    }
  }
}
/deep/.ant-pagination-prev,/deep/.ant-pagination-next {
  width: 30px;
  height: 30px;
  min-width: 30px;
  border-color: #EFEFEF;
  border-radius: 6px;
  a {
    border-radius: 6px;
  }
  // i {
  //   display: none;
  // }
}
/deep/.ant-pagination-item {
  width: 30px;
  height: 30px;
  min-width: 30px;
  text-align: center;
  border-radius: 6px;
  a {
    display: inline-block;
    line-height: 30px;
    padding: 0;
  }
}
/deep/.ant-pagination-item-active {
  background: #27BFA3;
  border-color: none;
  a {
    color: #fff;
  }
}
/deep/.anticon {
  vertical-align: 0;
}
</style>
